package com.common.dao.impl;

import java.util.List;

import com.common.dao.UserDAO;
import com.common.model.User;

public class UserDaoImpl extends BaseDAO implements UserDAO {
	
	@Override
	public boolean reg(User user) {
		return this.saveOrUpdate(user);
	}
	
	@Override
	public User bLogin(String username, String password) {
		StringBuffer sb = new StringBuffer("FROM User WHERE username = '")
		.append(username).append("' and password = '").append(password).append("' and status = 0");
		
		List<User> lst = findByHQL(sb.toString());
		if(lst.size()>0){
			return lst.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public List<User> list_baoxiu() {
		StringBuffer sb = new StringBuffer("FROM User where status =0");
		List<User> lst = findByHQL(sb.toString());
		return lst;
	}
	
	@Override
	public User wLogin(String username, String password) {
		StringBuffer sb = new StringBuffer("FROM User WHERE username = '")
		.append(username).append("' and password = '").append(password).append("' and status = 1");
		
		List<User> lst = findByHQL(sb.toString());
		if(lst.size()>0){
			return lst.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public List<User> list_weixiu() {
		StringBuffer sb = new StringBuffer("FROM User where status =1");
		List<User> lst = findByHQL(sb.toString());
		return lst;
	}
	
	@Override
	public List<User> list(String name) {
		StringBuffer sb;
		if(name !=null && !name.equals("")){
			 sb = new StringBuffer("FROM User WHERE name like '%")
			.append(name).append("%' and status =0");
		}else{
			 sb = new StringBuffer("FROM User where status =0");
		}
		
		List<User> lst = findByHQL(sb.toString());
		 return lst;
	}
	
	@Override
	public User load(int id) {
		return get(User.class, id);
	}
	
	@Override
	public boolean bupdate(User user) {
		StringBuffer sb;
		sb = new StringBuffer("update User set password ='").append(user.getPassword()).append("' where id = ").append(user.getId());
		
		try {
			executeHql(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	
	
	@Override
	public User login(String username, String password) {
		StringBuffer sb = new StringBuffer("FROM User WHERE username = '")
		.append(username).append("' and password = '").append(password).append("'");
		
		List<User> lst = findByHQL(sb.toString());
		if(lst.size()>0){
			return lst.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public boolean wDel(User user) {
		return deleteById(User.class, user.getId());
	}
	
	public boolean bDel(User user) {
		return deleteById(User.class, user.getId());
	}

	@Override
	public User load1(int id) {
		StringBuffer sb = new StringBuffer("FROM User WHERE id = ")
		.append(id);
		
		List<User> lst = findByHQL(sb.toString());
		if(lst.size()>0){
			return lst.get(0);
		}else{
			return null;
		}
	}

	@Override
	public List<User> list0(String username) {
		StringBuffer sb;
		sb = new StringBuffer("FROM User where    username ='").append(username).append("'");
		List<User> lst = findByHQL(sb.toString());
		 return lst;
	}

}
